package com.tc.resttemplate.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.http.client.ClientHttpRequestExecution;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.ClientHttpResponse;

import java.io.IOException;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Auther: tianchao
 * @Date: 2022/5/11 16:58
 * @Description:
 */
@Slf4j
public class MyOneClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
    AtomicInteger integer = new AtomicInteger();

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        log.info("one---before----执行拦截器");
        ClientHttpResponse execute =null;
        try {
            execute = execution.execute(request, body);
        }catch (Exception e){
            log.error("调用异常=-=======================   :{}",e);
            throw e;
        }
        log.info("one---after----执行拦截器");
        int i = integer.incrementAndGet();
//        if (i<10){
//            // execute.close();
//            log.error("解析发现异常 {}" , 1);
//            throw new RuntimeException("强硬抛异常");
//        }
        return execute;
    }
}
